#!/bin/bash
URL=https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip
source ../linux/download_unzip.sh "ik" $URL
# 创建数据目录
MODULE_PATH=/opt/module/es/single
DOCKER_NAME=es-single

# 删除数据目录及窗器
rm -rf $MODULE_PATH
if(( `docker ps -a | grep $DOCKER_NAME | wc -l` >0 ));then
    docker rm -f $DOCKER_NAME
fi


for folder in "data" "logs" "config" "plugins"; do
    if [ ! -d "$MODULE_PATH/$folder" ]; then
    mkdir -p $MODULE_PATH/$folder
    chmod 777 $MODULE_PATH/$folder
fi
done

# 开始下载

#拉取镜像
# docker pull elasticsearch:6.5.4
# 获取当前ip地址
IP=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 172.17.0.1|grep -v 10.0.2.15|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
# 移动ik到plugins 下
mv /opt/module/ik  $MODULE_PATH/plugins/
# 单机配置文件
sudo cat >$MODULE_PATH/config/elasticsearch.yml<<EOF
cluster.name: "docker-cluster"
network.host: 0.0.0.0
discovery.zen.minimum_master_nodes: 1
http.cors.enabled: true
http.cors.allow-origin: "*"
EOF


docker run -d \
    --privileged=true \
    -v /etc/localtime:/etc/localtime:ro \
    --name $DOCKER_NAME \
    --net host \
    -e "discovery.type=single-node" \
    -e "network.host=$IP" \
    -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
    -v $MODULE_PATH/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml  \
    -v $MODULE_PATH/logs:/usr/share/elasticsearch/logs  \
    -v $MODULE_PATH/data:/usr/share/elasticsearch/data \
    -v $MODULE_PATH/plugins:/usr/share/elasticsearch/plugins \
    elasticsearch:6.5.4
